/*  File Name	: TimeValue.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-6-27
 *  Description:
 */

#include "TimeValue.h"

const TimeValue TimeValue::zero;

TimeValue::TimeValue(int sec,int msec)
{
	this->sec_ = sec;
	this->msec_ = msec;
}

TimeValue::~TimeValue()
{

}

unsigned long long TimeValue::now_time_in_msec(void)
{
	timeval now;
	gettimeofday(&now, 0);
	unsigned long long msec = (unsigned long long)now.tv_sec*1000+now.tv_usec/1000;
	return msec;
}

unsigned long TimeValue::now_time_in_sec(void)
{
	timeval now;
	gettimeofday(&now, 0);
	return now.tv_sec;
}

//get now time as format YYYY-MM-DD HH:mm:ss
std::string TimeValue::now_time_str_sec(void)
{
	char now[64]={0};
	time_t t = time(0);
    strftime(now, 64,"%Y-%m-%d %H:%M:%S", localtime(&t));
    return now;
}

void TimeValue::set(int sec,int msec)
{
	this->sec_ = sec;
	this->msec_ = msec;
}

void TimeValue::sec(unsigned long sec)
{
	this->sec_ = sec;
}

unsigned long TimeValue::sec(void) const
{
	return this->sec_;
}

void TimeValue::msec(unsigned long msec)
{
	this->msec_ = msec;
}

unsigned long TimeValue::msec(void) const
{
	return this->msec_;
}

TimeValue &TimeValue::operator=(const TimeValue &tv)
{
	this->sec_ = tv.sec();
	this->msec_ = tv.msec();
	return *this;
}

//bool &TimeValue::operator==(const TimeValue &t1, const TimeValue &t2)
//{
//	return (t1.sec()==t2.sec() && t1.msec()==t2.msec());
//}
//
//bool &TimeValue::operator >(const TimeValue &t1,const TimeValue &t2)
//{
//	if(t1.sec()>t2.sec())
//	{
//		return true;
//	}
//	else if(t1.sec()==t2.sec() && t1.msec()>t2.msec())
//	{
//		return true;
//	}
//	return false;
//}
//
//bool &TimeValue::operator>=(const TimeValue &t1,const TimeValue &t2)
//{
//	if(t1.sec()>t2.sec())
//	{
//		return true;
//	}
//	else if(t1.sec()==t2.sec() && t1.msec()>=t2.msec())
//	{
//		return true;
//	}
//	return false;
//}
